package com.cq.health.jobs;

import com.cq.health.common.RedisConst;
import com.cq.health.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.JedisPool;

import java.util.Iterator;
import java.util.Set;

/**
 * @Author: 悠久的苍穹
 * @Date: 2020/6/1 13:50
 * @Description: 任务类
 * @Version: 1.0
 */
public class ClearJob {
    @Autowired
    private JedisPool jedisPool;
    /**
     * 任务内容
     * 清除图片
     */
    public void claerImageJob(){
        System.out.println("claerImageJob");
        //获取差值  计算redis中两个集合的差值，获取垃圾图片名称
        Set<String> stringSet = jedisPool.getResource().sdiff(RedisConst.SETMEAL_PIC_RESOURCES, RedisConst.SETMEAL_PIC_DB_RESOURCES);
        Iterator<String> iterator = stringSet.iterator();
        while(iterator.hasNext()){
            String imgName = iterator.next();
            System.out.println("需要删除的图片"+imgName);
            //删除图片服务器中的图片文件
            QiniuUtils.deleteFileFromQiniu(imgName);
            //删除redis中的数据
            jedisPool.getResource().srem(RedisConst.SETMEAL_PIC_RESOURCES,imgName);
        }
    }
}
